#!/bin/bash

if [ ! -e /nginx/conf/nginx.conf ]; then
  cp /usr/local/nginx/conf/nginx.conf /nginx/conf
  sed -i "s/mime.types/\/usr\/local\/nginx\/conf\/mime.types/" /nginx/conf/nginx.conf
  sed -i "s/#error_log  logs\/error.log;/error_log  \/nginx\/logs\/error.log;/" /nginx/conf/nginx.conf
  sed -i "s/#error_log  logs\/error.log  notice;/error_log  \/nginx\/logs\/error.log  notice;/" /nginx/conf/nginx.conf
  sed -i "s/#error_log  logs\/error.log  info;/error_log  \/nginx\/logs\/error.log  info;/" /nginx/conf/nginx.conf
  sed -i "s/#pid        logs\/nginx.pid;/pid        \/nginx\/logs\/nginx.pid;/" /nginx/conf/nginx.conf
  sed -i "s/root   html;/root   \/nginx\/www\/dynamic;/" /nginx/conf/nginx.conf
fi

if [ ! -e /nginx/www/dynamic ]; then
    cd /nginx/www;
    git clone -b $BRANCH $REPOSITORY dynamic;
fi

/usr/local/nginx/sbin/nginx -c /nginx/conf/nginx.conf

while true;
    do
        cd /nginx/www;
        git fetch --all > /dev/null 2>&1 ;
        git reset --hard origin/$BRANCH > /dev/null 2>&1 ;
        git pull > /dev/null 2>&1 ;
        sleep 5;
    done